﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Admin.NET.Application.Service;
using Furion.FriendlyException;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 系列服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class car_colorService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_color> _rep;
    public car_colorService(SqlSugarRepository<car_color> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_tag>> Page(car_colorInput input)
    {
        //var query= _rep.AsQueryable().Where(m=>m.IsDelete==false)
        //          .LeftJoin<car_color>((u, pca) => u.pid == pca.Id)
        //            .WhereIF(!string.IsNullOrWhiteSpace(input.colorName), u => u.colorName.Contains(input.colorName.Trim()))
        //            .WhereIF(input.xilieId>0, u => u.xilieId == input.xilieId)
        //            .WhereIF(!string.IsNullOrWhiteSpace(input.colorValue), u => u.colorValue.Contains(input.colorValue.Trim()))
        //            .WhereIF(!string.IsNullOrWhiteSpace(input.imgurl), u => u.imgurl.Contains(input.imgurl.Trim()))
        //            .WhereIF(input.carid>0, u => u.carid == input.carid)
        //            .WhereIF(input.pid>0, u => u.pid == input.pid)
        //                .Select((u, pca) => new car_colorOutput
        //                {

        //                    colorName = u.colorName,
        //                    Id = u.Id,
        //                    pid = u.pid,
        //                    pname = pca.colorName,
        //                });

        var query = _rep.Context.Queryable<car_tag>()
        .Where(m => m.IsDelete == false && m.type == (int?)car_tagType.color);

 
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_colorInput input)
    {
        var entity = input.Adapt<car_color>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_colorInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_colorInput input)
    {
        var entity = input.Adapt<car_color>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_color> Get([FromQuery] QueryByIdcar_colorInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取系列列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_colorOutput>> List([FromQuery] car_colorInput input)
    {
        return await _rep.AsQueryable().Select<car_colorOutput>().ToListAsync();
    }



    /// <summary>
    ///  颜色系列
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "car_colorPidDropdown"), HttpGet]
    public List<TreeModel> car_colorPidDropdown()
    {
        var lists = _rep.Context.Queryable<car_color>().Where(m => m.pid == 0 && m.IsDelete == false).ToList();



        List<TreeModel> reList = new List<TreeModel>();
        foreach (var item in lists)
        {
            reList.Add(new TreeModel()
            {
                label = item.colorName,
                value = item.Id,
                //children = lists.Where(m => m.pid == item.Id).Select(u => new TreeModelItem
                //{
                //    label = u.carname,
                //    value = u.Id,

                //}).ToList(),
            });
        }
        return reList;
        //return await _rep.Context.Queryable<car_car>().GroupBy(m=>m.pid)
        //        .Select(u => new
        //        {
        //            Label = u.carname,
        //            Value = u.Id
        //        }
        //        ).ToListAsync();
    }






}

